//
//  FansFocusViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/2/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FansFocusViewController.h"
#import "StarInfomationViewController.h"
#import "FocusStarCell.h"
#import "FocusStarModel.h"

@interface FansFocusViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    NSMutableArray *_starlistArr;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation FansFocusViewController
static NSString *focusStarCell = @"focusStarCell";

#pragma mark -- 关注明星列表请求
- (void)sendFocusStarListRequest {
    NSDictionary *dict = @{
                           @"udid":User_ID,
                           @"token":TOKEN
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kFoucsStarList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([KString(responseObject[@"code"]) isEqualToString:@"200"]) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *dic in array) {
                FocusStarModel *model = [[FocusStarModel alloc] initWithDict:dic];
                [_starlistArr addObject:model];
            }
        }
        [self.collectionView reloadData];
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

#pragma mark --  lazy
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"FocusStarCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:focusStarCell];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我关注的明星";
    [self sendFocusStarListRequest];
    [self setUpUI];
    
    // Do any additional setup after loading the view.
}

- (void)setUpUI {
    _starlistArr = [NSMutableArray arrayWithCapacity:1];
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

#pragma mark -- collectionView Delegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _starlistArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FocusStarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:focusStarCell forIndexPath:indexPath];
    cell.focusModel = _starlistArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FocusStarModel *model = _starlistArr[indexPath.row];
    StarInfomationViewController *infoVC = [StarInfomationViewController new];
    infoVC.s_uid = model.s_udid;
    [self.navigationController pushViewController:infoVC animated:YES];
}
//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(55,70);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
